package com.yourwork.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.yourwork.model.Clientele;
import com.yourwork.model.Consumption;
import com.yourwork.model.Recharge;
import com.yourwork.model.ServiceProject;
import com.yourwork.service.ConsumptionService;
import com.yourwork.service.ServiceProjectService;
@Controller
@RequestMapping(value="/consumption")
/**
 * 消费记录*/
public class ConsumptionController {
	@Autowired
	@Qualifier(value="recordService")
	private ConsumptionService recordService;
	@Autowired
	@Qualifier(value="serviceItemService")
	private ServiceProjectService serviceItemService;
	
	//	分页查询
	@RequestMapping(value="/select",method={RequestMethod.GET})
	public String queryRecord(Model m,HttpSession session,
			@RequestParam(name="page",required=false,defaultValue="0") int page,
			@RequestParam(name="size",required=false,defaultValue="10") int size){
		try{int count = recordService.countRecord();//记录总条数
		if(page<=(count/size)&&count%size!=0){page=page+0;}
		else{if(count%size==0){page=count/size-1;}
		else{page = count/size;}}
		Page<Consumption> pageRecord = recordService.queryRecord(page,size);//查询总的Record表中所有的记录
		List<Consumption> list = new ArrayList<Consumption>();
		for(Consumption r: pageRecord){
			list.add(r);
		}
		List<ServiceProject> pageServiceItem = serviceItemService.queryServiceItemAll(true);//查询在使用的服务类型
		int maxPage = recordService.queryMaxPage(page,size);//最大页数
		m.addAttribute("maxPage", maxPage);//保存最大页数
		m.addAttribute("page", page);//当前页数
		m.addAttribute("size", size);//当前每页显示的条数
		addAttribute(m, "list", list, true);//前端遍历的Record的记录
		m.addAttribute("pageServiceItem",pageServiceItem);//多选框遍历所有的服务类型
		m.addAttribute("count", count);//记录总条数
		m.addAttribute("remind", session.getAttribute("remind"));//提示信息
		m.addAttribute("managerNumber", session.getAttribute("managerNumber"));//当前登陆的
		m.addAttribute("sumPayavle",session.getAttribute("sumPayavle"));//应付的总额
		session.setAttribute("remind", "");//清空提示信息
		session.setAttribute("sumPayavle",0.00);//清空应付总额
		return "/work/service";}catch (Exception e) {
			// TODO: handle exception
			return "/work/service";
		}
		
	}

	 // 添加消费记录
	@RequestMapping(value="/addRecord",method={RequestMethod.POST})
	public String insertRecord(@ModelAttribute(name="record") Consumption record,
			HttpSession session,@RequestParam(name="serviceIemId") int[] serviceIemId,
			@RequestParam(name="number") String [] number){
		recordService.insertRecord(record,serviceIemId,number,session);
		return "redirect:/consumption/select";
	}
	private void addAttribute(Model m,String attributeName,Object attributeValue,boolean replace){
		if(replace==true){//判断是否添加attribute
			m.addAttribute(attributeName, attributeValue);
		}else{
			if(!m.containsAttribute(attributeName)){//判断是否已经存在的attribute
				m.addAttribute(attributeName, attributeValue);
			}
		}
	}
}
